package com.yun.service.impl;

import com.yun.entity.MClass;
import com.yun.entity.MExam;
import com.yun.entity.MSubject;
import com.yun.mapper.MClassMapper;
import com.yun.mapper.MExamMapper;
import com.yun.mapper.MSubjectMapper;
import com.yun.service.MarkingProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (MSubject)表服务实现类
 *
 * @author makejava
 * @since 2021-10-27 14:04:35
 */
@Service("MarkingProcessService")
public class MarkingProcessServiceImpl implements MarkingProcessService {
    @Autowired
    private MSubjectMapper mSubjectMapper;
    @Autowired
    private MClassMapper mClassMapper;
    @Autowired
    private MExamMapper mExamMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param examSubId 主键
     * @return 实例对象
     */
    @Override
    public MSubject queryById(Integer examSubId) {
        return this.mSubjectMapper.queryById(examSubId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MSubject> queryAllByLimit(int offset, int limit) {
        return this.mSubjectMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param mSubject 实例对象
     * @return 实例对象
     */
    @Override
    public MSubject insert(MSubject mSubject) {
        this.mSubjectMapper.insert(mSubject);
        return mSubject;
    }

    /**
     * 修改数据
     *
     * @param mSubject 实例对象
     * @return 实例对象
     */
    @Override
    public MSubject update(MSubject mSubject) {
        this.mSubjectMapper.update(mSubject);
        return this.queryById(mSubject.getExamSubId());
    }

    /**
     * 通过主键删除数据
     *
     * @param examSubId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer examSubId) {
        return this.mSubjectMapper.deleteById(examSubId) > 0;
    }

    /**
     * 通过考试ID查询考试科目
     * 查询到结果：
     * -- map.put("code", 1001);map.put("msg", "查询成功");
     * 查询为空：
     * -- map.put("code", 1002);map.put("msg", "未查到数据");
     * 查询异常：
     * -- map.put("code", 1004);map.put("msg", "异常请联系管理员");
     *
     * @return
     */
    @Override
    public List<MSubject> querySubNm(Integer examId) {
        List<MSubject> mSubjectListinfo = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();

        try {
            //查询出考试科目
            mSubjectListinfo = mSubjectMapper.querySubId(examId);
            if (mSubjectListinfo != null) {
                map.put("mSubjectListinfo", mSubjectListinfo);
                map.put("code", 1001);
                map.put("msg", "查询成功");
            } else {
                map.put("code", 1002);
                map.put("msg", "查询失败");
            }
        } catch (Exception e) {
            map.put("code", 1004);
            map.put("msg", "异常请联系管理员");
            e.printStackTrace();
        }
        return mSubjectListinfo;
    }

    /**
     * 通过考试ID查询出所有考试班级
     * 查询到结果：
     * -- map.put("code", 1001);map.put("msg", "查询成功");
     * 查询为空：
     * -- map.put("code", 1002);map.put("msg", "未查到数据");
     * 查询异常：
     * -- map.put("code", 1004);map.put("msg", "异常请联系管理员");
     *
     * @param examId
     * @return
     */
    @Override
    public List<MClass> queryClaNm(Integer examId) {
        List<MClass> mClassList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();

        try {
            //查询出考试班级
            mClassList = mClassMapper.queryClaNm(examId);
            if (mClassList != null) {
                map.put("mClassList", mClassList);
                map.put("code", 1001);
                map.put("msg", "查询成功");
            } else {
                map.put("code", 1002);
                map.put("msg", "查询失败");
            }
        } catch (Exception e) {
            map.put("code", 1004);
            map.put("msg", "异常请联系管理员");
            e.printStackTrace();
        }
        return mClassList;
    }

    //查询考试ID和考试名称
    @Override
    public List<MExam> queryExamIdAndExamNm() {
        List<MExam> mExamList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();

        try {
            //查询出考试ID和考试名称
            mExamList = mExamMapper.queryExamIdAndExamNm();
            if (mExamList != null) {
                map.put("mExamList", mExamList);
                map.put("code", 1001);
                map.put("msg", "查询成功");
            } else {
                map.put("code", 1002);
                map.put("msg", "查询失败");
            }
        } catch (Exception e) {
            map.put("code", 1004);
            map.put("msg", "异常请联系管理员");
            e.printStackTrace();
        }
        return mExamList;
    }


}